﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SES.CMS.BL;
using SES.CMS.DO;
using System.Data;

namespace SES.CMS.AdminCP
{
    public partial class SearchPrice : System.Web.UI.Page
    {
        SearchPriceDO objSearchPrice = new SearchPriceDO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                view();
            }
        }
        void view()
        {
            gvSearchPrice.DataSource = new SearchPriceBL().SelectAll();
            gvSearchPrice.DataBind();
            pnView.Visible = true;
            pnInsert.Visible = false;
        }
        protected void gvSearchPrice_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            new SearchPriceBL().Delete(new SearchPriceDO { Id = Convert.ToInt32(gvSearchPrice.DataKeys[e.RowIndex].Value) });

            Functions.Alert("Xóa bản tin thành công!", Request.Url.ToString());
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            pnView.Visible = false;
            pnInsert.Visible = true;
            txtFrmPrice.Text = "";
            txtToPrice.Text = "";
        }
        protected void gvSearchPrice_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvSearchPrice.PageIndex = e.NewPageIndex;
        }
        private void initObject()
        {
            objSearchPrice = new SearchPriceBL().Select(objSearchPrice);
            objSearchPrice.FromPrice =Convert.ToDouble( txtFrmPrice.Text);
            objSearchPrice.ToPrice =Convert.ToDouble( txtToPrice.Text);
           
        }
        protected void btnReset_Click(object sender, EventArgs e)
        {
            view();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            initObject();

            if(hdfID.Value.Equals(""))
            {
                new SearchPriceBL().Insert(objSearchPrice);
            }
            else
            {
               // int Id = int.Parse(gvSearchPrice.DataKeys[gvSearchPrice.SelectedIndex].Value.ToString());
                int Id = int.Parse(hdfID.Value);
                objSearchPrice.Id = Id;
                new SearchPriceBL().Update(objSearchPrice);
                hdfID.Value = "";
            }
            Functions.Alert("Cập nhật thành công!");
            view();
        }

        protected void gvSearchPrice_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument);
            objSearchPrice.Id = id;
            hdfID.Value = id.ToString();
            pnView.Visible = false;
            pnInsert.Visible = true;
            switch (e.CommandName)
            {
                case "Select":
                    objSearchPrice = new SearchPriceBL().Select(objSearchPrice);
                    txtFrmPrice.Text =Convert.ToString( objSearchPrice.FromPrice);
                    txtToPrice.Text =Convert.ToString( objSearchPrice.ToPrice);
                    break;
            }
        }

    }
}